package com.xujf.sym

import android.app.TabActivity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.TabHost
import android.widget.Toast
import com.xujf.sym.ui.activity.MainActivity
import com.xujf.sym.ui.activity.TestActivity
import kotlinx.android.synthetic.main.activity_main.*

/**
 * Created by xujf on 2018/2/5.
 * 主页
 */
class MainTabActivity : TabActivity(), View.OnClickListener {

    private lateinit var mTabHost: TabHost
    // 首次按下返回键时间戳
    private var firstBackPressedTime = 0L

    override fun onCreate(savedInstanceState: Bundle?) {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//            window.statusBarColor = resources.getColor(R.color.compatible_translucent_status)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.statusBarColor = resources.getColor(R.color.compatible_translucent_status)
        }

        setContentView(R.layout.activity_main)
        initTab()
    }

    private fun initTab() {
        mTabHost = this.tabHost
        val tab1Spec = mTabHost.newTabSpec("tab1")
        val tab2Spec = mTabHost.newTabSpec("tab2")
        val tab3Spec = mTabHost.newTabSpec("tab3")
        val tab4Spec = mTabHost.newTabSpec("tab4")

        tab1Spec.setIndicator("tab1").setContent(Intent(this, MainActivity::class.java))
        tab2Spec.setIndicator("tab2").setContent(Intent(this, TestActivity::class.java))
        tab3Spec.setIndicator("tab3").setContent(Intent(this, TestActivity::class.java))
        tab4Spec.setIndicator("tab4").setContent(Intent(this, TestActivity::class.java))

        mTabHost.addTab(tab1Spec)
        mTabHost.addTab(tab2Spec)
        mTabHost.addTab(tab3Spec)
        mTabHost.addTab(tab4Spec)

        ll_tab1.setOnClickListener(this)
        ll_tab2.setOnClickListener(this)
        ll_tab3.setOnClickListener(this)
        ll_tab4.setOnClickListener(this)

        mTabHost.setOnTabChangedListener { arg0 ->
            if (arg0 == "tab1") {
                iv_tab1.setImageResource(R.drawable.ic_tab1)
                iv_tab2.setImageResource(R.drawable.ic_tab1)
                iv_tab3.setImageResource(R.drawable.ic_tab1)
                iv_tab4.setImageResource(R.drawable.ic_tab1)

                tv_tab1.setTextColor(resources.getColor(R.color.orange))
                tv_tab2.setTextColor(resources.getColor(R.color.tab_unselect))
                tv_tab3.setTextColor(resources.getColor(R.color.tab_unselect))
                tv_tab4.setTextColor(resources.getColor(R.color.tab_unselect))
            } else if (arg0 == "tab2") {
                iv_tab1.setImageResource(R.drawable.ic_tab1)
                iv_tab2.setImageResource(R.drawable.ic_tab1)
                iv_tab3.setImageResource(R.drawable.ic_tab1)
                iv_tab4.setImageResource(R.drawable.ic_tab1)

                tv_tab1.setTextColor(resources.getColor(R.color.tab_unselect))
                tv_tab2.setTextColor(resources.getColor(R.color.orange))
                tv_tab3.setTextColor(resources.getColor(R.color.tab_unselect))
                tv_tab4.setTextColor(resources.getColor(R.color.tab_unselect))
            } else if (arg0 == "tab3") {
                iv_tab1.setImageResource(R.drawable.ic_tab1)
                iv_tab2.setImageResource(R.drawable.ic_tab1)
                iv_tab3.setImageResource(R.drawable.ic_tab1)
                iv_tab4.setImageResource(R.drawable.ic_tab1)

                tv_tab1.setTextColor(resources.getColor(R.color.tab_unselect))
                tv_tab2.setTextColor(resources.getColor(R.color.tab_unselect))
                tv_tab3.setTextColor(resources.getColor(R.color.orange))
                tv_tab4.setTextColor(resources.getColor(R.color.tab_unselect))
            } else {
                iv_tab1.setImageResource(R.drawable.ic_tab1)
                iv_tab2.setImageResource(R.drawable.ic_tab1)
                iv_tab3.setImageResource(R.drawable.ic_tab1)
                iv_tab4.setImageResource(R.drawable.ic_tab1)

                tv_tab1.setTextColor(resources.getColor(R.color.tab_unselect))
                tv_tab2.setTextColor(resources.getColor(R.color.tab_unselect))
                tv_tab3.setTextColor(resources.getColor(R.color.tab_unselect))
                tv_tab4.setTextColor(resources.getColor(R.color.orange))
            }
        }
    }

    private fun setTabOrNot(i: Int) {
        if (mTabHost.currentTab != i) {
            mTabHost.currentTab = i
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            ll_tab1 -> {
                setTabOrNot(0)
            }
            ll_tab2 -> {
                setTabOrNot(1)
            }
            ll_tab3 -> {
                setTabOrNot(2)
            }
            ll_tab4 -> {
                setTabOrNot(3)
            }
        }
    }

    /**
     * TabActivity重写onBackPressed()会失效
     */
    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
            if (!MainActivity.isDrawerOpen) {
                val secondBackPressedTime = System.currentTimeMillis()
                if (secondBackPressedTime - firstBackPressedTime > 2000) {
                    Toast.makeText(this, "再按一次退出" + resources.getString(R.string.app_name), Toast.LENGTH_SHORT).show()
                    firstBackPressedTime = secondBackPressedTime
                } else {
                    finish()
                }
            }
            return false
        }
        return super.dispatchKeyEvent(event)
    }
}